//
//  CreatePropertyViewController.m
//  YunGui
//
//  Created by HanenDev on 15/11/12.
//  Copyright © 2015年 hanen. All rights reserved.
//

#import "CreatePropertyViewController.h"
#import "BankTypeModel.h"
#import "UIWindow+YUBottomPoper.h"
#import "ZLAlertView.h"
#import "ImageButton.h"

@interface CreatePropertyViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>

{
    NSArray      *_labelArray;
    UIScrollView * scrollView;
    
    UITableView  *_tableView;

    //银行列表
    UIPickerView    *_bankPicker;
    UIView          *_bankListView;
    UIView          *_backView;
    //地区列表
    ImageButton *locationBtn;
    UIView *_placeView;
    UIPickerView *_placePickerView;
    
    AMapSearchAPI *_search;
    
    CGFloat  lontitu;//经度
    CGFloat  latitu;//纬度
    
    BOOL    isBank;
    
    NSString *proStr;
    NSString *city;
    NSString *regionStr;
    
    int regionID;
    int bankTypeID;
    int bankNameID;
}

@property(nonatomic,strong)UITextField *tabTF;
@property(nonatomic,strong)UITextField *propertyTF;
@property(nonatomic,strong)UITextField *nameTF;
@property(nonatomic,strong)UITextField *telTF;
@property(nonatomic,strong)UITextField *locationTF;
@property(nonatomic,strong)UITextField *detailTF;
@property(nonatomic,strong)UITextField *typeTF;
@property(nonatomic,strong)UITextField *bankTF;
@property(nonatomic,strong)UITextField *bankIDTF;
@property(nonatomic,strong)UITextField *houseHolderTF;
@property(nonatomic,strong)UITextField *addrTF;
@property(nonatomic,strong)ImageButton    *bankTypeBtn;
@property(nonatomic,strong)ImageButton    *bankListBtn;

@property (strong, nonatomic) NSMutableArray *provinceArray;
@property (strong, nonatomic) NSMutableArray *cityArray;
@property (strong, nonatomic) NSMutableArray *regionArray;
@property (strong, nonatomic) NSMutableArray *selectedArray;

@property(nonatomic,strong)NSMutableArray *bankListArr;
@property(nonatomic,strong)NSMutableArray *bankTypeArr;

@end

@implementation CreatePropertyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = GrayBackColor;
    self.navigationItem.hidesBackButton = YES;
    [ViewTool setNavigationBar:self.navigationController.navigationBar];
    self.navigationItem.title = @"创建物业";
    
    self.navigationItem.leftBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"back" WithAction:@selector(goToBack)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(referClick:)];
    
    [self initViews];
    [self initCardDictionary];
    
    _search = [[AMapSearchAPI alloc] init];
    _search.delegate = self;
    
    //获取省市区
    [self initCity];
    
    [self initData];//获取银行接口
    [self initBankType];//获取账户类型接口
    
    
    _provinceArray = [NSMutableArray array];
    _cityArray   = [NSMutableArray array];
    _regionArray = [NSMutableArray array];
    
    _bankTypeArr = [NSMutableArray array];
    _bankListArr = [NSMutableArray array];
    
}
- (void)initCity{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"app.region.all" forKey:@"method"];
    [dic setObject:@0 forKey:@"isCache"];
    
    [DataTool postWithUrl:HEAD_URL parameters:[DataTool addSignToDic:dic] success:^(id data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"%@+++shengshiqu",jsonDic);
        NSDictionary *dataDic = jsonDic[@"data"];
        NSMutableArray *arr = dataDic[@"allRegion"];
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *proDic = arr[i];
            if (proDic) {
                [_provinceArray addObject:proDic];
            }
            self.selectedArray = _provinceArray.firstObject[@"city"];
            if (self.selectedArray.count > 0) {
                self.cityArray = _provinceArray.firstObject[@"city"];
            }
            if(self.cityArray.count> 0){
                self.regionArray = self.cityArray.firstObject[@"district"];
            }
            
        }
        
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];

}
- (void)initData{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"app.property.bank.list" forKey:@"method"];
    [dic setObject:@0 forKey:@"isCache"];
    
    [DataTool postWithUrl:HEAD_URL parameters:[DataTool addSignToDic:dic] success:^(id data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = jsonDic[@"data"];
        NSMutableArray *arrr = dataDic[@"bankList"];
//        for (int i=0; i<arrr.count; i++) {
//            NSDictionary *dicc = arrr[i];
//            [_bankListArr addObject:dicc[@"bank_name"]];
//        }
        _bankListArr = arrr;
//        NSLog(@"%@",jsonDic);
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}
- (void)initBankType{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"app.property.bank.type" forKey:@"method"];
    [dic setObject:@0 forKey:@"isCache"];
    
    [DataTool postWithUrl:HEAD_URL parameters:[DataTool addSignToDic:dic] success:^(id data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = jsonDic[@"data"];
        NSMutableArray *arrr = dataDic[@"bankType"];
        _bankTypeArr = arrr;
//        NSLog(@"%@",jsonDic);
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

- (void)initCardDictionary{
    
    NSArray *array;
    if (_dictionary.count>0) {
        
        array = [_dictionary objectForKey:@"comp"];
        if (array.count > 0) {
            _propertyTF.text = array[0];
        }
        
        array = [_dictionary objectForKey:@"name"];
        if (array.count > 0) {
            _nameTF.text = array[0];
        }
        
        array = [_dictionary objectForKey:@"mobile"];
        if (array.count > 0) {
            _telTF.text = array[0];
        }else{
            array = [_dictionary objectForKey:@"tel"];
            if(array.count > 0){
                _telTF.text = array[0];
            }
        }
        array = [_dictionary objectForKey:@"addr"];
        if (array.count > 0) {
            _detailTF.text = array[0];
        }
        
        
    }else{
        
    }
    
}
- (void)initViews{
    scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:scrollView];
    
    NSArray *imArr = @[@"Btn_Normal_Tongqian",@"Btn_Normal_Xiaoquming",@"Btn_Nor_Lianxiren",@"Btn_Normal_Dianhua",@"Btn_Normal_Diqu",@"Btn_Normal_Didian",@"Btn_Normal_Liexing",@"Btn_Normal_Yinhang",@"Btn_Normal_Zhanghao",@"Btn_Normal_Huming",@"Btn_Normal_Dizhilv"];
    _labelArray=@[@"标签",@"物业名称",@"联系人",@"联系电话",@"所属地区",@"详细地址",@"账户类型",@"所属银行",@"银行账号",@"银行户名",@"开户银行详细地址"];
    
    CGFloat startX = [UIView getWidth:20];
    CGFloat imgW = 20;
    CGFloat labX = startX + imgW +15;
    CGFloat labH = 40;
    CGFloat labY = 10;
    CGFloat height = labH - 2*labY;
    CGFloat grayH = 15;
    CGFloat labW  = 80;
    CGFloat tfX = labX + labW +[UIView getWidth:15];
    CGFloat tfW = kScreenWidth - tfX;
    CGFloat ButtonW = tfW;
    CGFloat tfH = 2*height;
    
    //第一区
    UIView *bgView2 = [self getBgViewWith:CGRectMake(0, grayH, kScreenWidth, labH*3)];
    [scrollView addSubview:bgView2];
    
    UIView *line2 = [self getLineWith:CGRectMake(0, 0, kScreenWidth, 1)];
    [bgView2 addSubview:line2];
    
    for (int i=0; i<3; i++) {
        UIImageView *labImg = [self imageWith:CGRectMake(startX, line2.maxY + labY + i*labH, imgW, imgW) withName:imArr[i+1]];
        [bgView2 addSubview:labImg];
        
        UILabel *labLabel = [self getLabelWith:CGRectMake(labX, line2.maxY + labY + i*labH, labW, height) WithTitle:_labelArray[i+1]];
        [bgView2 addSubview:labLabel];
        
        UIView *line = [self getLineWith:CGRectMake(labX, line2.maxY + labH*(i+1) - 1, kScreenWidth, 1)];
        [bgView2 addSubview:line];
        
        if (i == 2) {
            line.frame = CGRectMake(0, line2.maxY + labH*(i+1)-1, kScreenWidth, 1);
            line.tag = 1000;
        }
    }
    
    _propertyTF = [self addTextFieldWithFrame:CGRectMake(tfX, line2.maxY, tfW, tfH) AndStr:@"必填"];
    [bgView2 addSubview:_propertyTF];
    
    _nameTF = [self addTextFieldWithFrame:CGRectMake(tfX, _propertyTF.maxY, tfW, tfH) AndStr:@"必填"];
    [bgView2 addSubview:_nameTF];
    
    _telTF = [self addTextFieldWithFrame:CGRectMake(tfX, _nameTF.maxY , tfW, tfH) AndStr:@"必填"];
    _telTF.keyboardType = UIKeyboardTypeNumberPad;
    [bgView2 addSubview:_telTF];
    
    
    
    //第二区
    UIView *bgView3 = [self getBgViewWith:CGRectMake(0, bgView2.maxY +grayH, kScreenWidth, labH*2)];
    [scrollView addSubview:bgView3];
    
    UIView *line3 = [self getLineWith:CGRectMake(0, 0, kScreenWidth, 1)];
    [bgView3 addSubview:line3];
    
    for (int i=0; i<2; i++) {
        UIImageView *labImg = [self imageWith:CGRectMake(startX, line3.maxY + labY + i*labH, imgW, imgW) withName:imArr[i+4]];
        [bgView3 addSubview:labImg];
        
        UILabel *labLabel = [self getLabelWith:CGRectMake(labX, line3.maxY + labY + i*labH, labW, height) WithTitle:_labelArray[i+4]];
        [bgView3 addSubview:labLabel];
        
        UIView *line = [self getLineWith:CGRectMake(labX, line3.maxY + labH *(i+1) - 1, kScreenWidth, 1)];
        [bgView3 addSubview:line];
        
        if (i == 1) {
            line.frame = CGRectMake(0, line3.maxY + labH *(i+1)-1, kScreenWidth, 1);
        }
    }
    
    _locationTF = [self addTextFieldWithFrame:CGRectMake(tfX, line3.maxY , tfW - 40, tfH) AndStr:@"请填写"];
    _locationTF.enabled = NO;
    [bgView3 addSubview:_locationTF];
    
    
    locationBtn = [[ImageButton alloc]initWithFrame:CGRectMake(kScreenWidth - ButtonW, line3.maxY, ButtonW, labH)];
    [locationBtn setImage:[UIImage imageNamed:@"Btn_xiangXia"] forState:UIControlStateNormal];
    [locationBtn addTarget:self action:@selector(clickToSelectBank:) forControlEvents:UIControlEventTouchUpInside];
    locationBtn.tag = 121212;
    locationBtn.selected = NO;
    [bgView3 addSubview:locationBtn];
    
    _detailTF = [self addTextFieldWithFrame:CGRectMake(tfX, _locationTF.maxY , tfW, tfH) AndStr:@"必填"];
    [bgView3 addSubview:_detailTF];
    
    
    //第三区
    UIView *bgView4 = [self getBgViewWith:CGRectMake(0, bgView3.maxY +grayH, kScreenWidth, labH*5)];
    [scrollView addSubview:bgView4];
    
    UIView *line4 = [self getLineWith:CGRectMake(0, 0, kScreenWidth, 1)];
    [bgView4 addSubview:line4];
    
    for (int i=0; i<5; i++) {
        UIImageView *labImg = [self imageWith:CGRectMake(startX, line4.maxY + labY + i*labH, imgW, imgW) withName:imArr[i+6]];
        [bgView4 addSubview:labImg];
        
        UILabel *labLabel = [self getLabelWith:CGRectMake(labX, line4.maxY + labY + i*labH, labW, height) WithTitle:_labelArray[i+6]];
        [bgView4 addSubview:labLabel];
        
        UIView *line = [self getLineWith:CGRectMake(labX, line4.maxY + labH*(i+1) - 1, kScreenWidth, 1)];
        [bgView4 addSubview:line];
        
        if (i == 4) {
            labLabel.frame = CGRectMake(labX, line4.maxY + labY + i*labH, 2*labW -20, height);
            line.frame = CGRectMake(0, line4.maxY + labH *(i+1)-1, kScreenWidth, 1);
        }
    }
    //
    _typeTF = [self addTextFieldWithFrame:CGRectMake(tfX, line4.maxY, tfW, tfH) AndStr:@"请填写"];
    _typeTF.enabled = NO;
    [bgView4 addSubview:_typeTF];
    
    _bankTypeBtn = [[ImageButton alloc]initWithFrame:CGRectMake(kScreenWidth -ButtonW, line4.maxY, ButtonW, labH)];
    [_bankTypeBtn addTarget:self action:@selector(clickToSelectBank:) forControlEvents:UIControlEventTouchUpInside];
    [_bankTypeBtn setImage:[UIImage imageNamed:@"Btn_xiangXia"] forState:UIControlStateNormal];
    _bankTypeBtn.selected = NO;
    _bankTypeBtn.tag = 10001;
    _bankTypeBtn.userInteractionEnabled = YES;
    [bgView4 addSubview:_bankTypeBtn];
    //
    _bankTF = [self addTextFieldWithFrame:CGRectMake(tfX, _typeTF.maxY , tfW, tfH) AndStr:@"请填写"];
    _bankTF.enabled = NO;
    [bgView4 addSubview:_bankTF];
    
    _bankListBtn = [[ImageButton alloc]initWithFrame:CGRectMake(kScreenWidth - ButtonW, _bankTF.y, ButtonW, labH)];
    [_bankListBtn addTarget:self action:@selector(clickToSelectBank:) forControlEvents:UIControlEventTouchUpInside];
    [_bankListBtn setImage:[UIImage imageNamed:@"Btn_xiangXia"] forState:UIControlStateNormal];
    _bankListBtn.selected = NO;
    _bankListBtn.tag = 10002;
    _bankListBtn.userInteractionEnabled = YES;
    [bgView4 addSubview:_bankListBtn];
    
    _bankIDTF = [self addTextFieldWithFrame:CGRectMake(tfX, _bankTF.maxY , tfW, tfH) AndStr:@"必填"];
    _bankIDTF.keyboardType = UIKeyboardTypeNumberPad;
    [bgView4 addSubview:_bankIDTF];
    
    _houseHolderTF = [self addTextFieldWithFrame:CGRectMake(tfX, _bankIDTF.maxY , tfW, tfH) AndStr:@"必填"];
    [bgView4 addSubview:_houseHolderTF];
    
    _addrTF = [self addTextFieldWithFrame:CGRectMake(tfX +[UIView getWidth:40], _houseHolderTF.maxY , tfW, tfH) AndStr:@"必填"];
    _addrTF.adjustsFontSizeToFitWidth = YES;
    [bgView4 addSubview:_addrTF];
    
    
    UILabel *exampleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, bgView4.maxY + labY, kScreenWidth - [UIView getWidth:15], 12)];
    exampleLabel.text = @"* 例如:上海张杨路支行";
    exampleLabel.textColor = UIColorFromRGB(0x626166);
    exampleLabel.textAlignment = NSTextAlignmentRight;
    exampleLabel.font = [UIFont systemFontOfSize:13.0f];
    [scrollView addSubview:exampleLabel];
    
    scrollView.contentSize = CGSizeMake(kScreenWidth, exampleLabel.maxY + 10);
    
    [self creatBankListPickerView];
    
    [self creatCityPickerView];
}
- (UIImageView *)imageWith:(CGRect)frame withName:(NSString *)str{
    UIImageView *img = [[UIImageView alloc]initWithFrame:frame];
    img.image = [UIImage imageNamed:str];
    return img;
}
- (UILabel *)getLabelWith:(CGRect)frame WithTitle:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = title;
    label.textColor = GRAYCOLOR;
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [ViewTool getFont:16.0f];
    return label;
}
-(UIView *)getLineWith:(CGRect)frame
{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = GRAYLINECOLOR;
    return view;
}
-(UIView *)getBgViewWith:(CGRect)frame
{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}
-(UITextField *)addTextFieldWithFrame:(CGRect)frame AndStr:(NSString *)placeholder
{
    UITextField *textF=[[UITextField alloc]initWithFrame:frame];
    textF.userInteractionEnabled = YES;
    textF.textColor = BLACKCOLOR;
    textF.placeholder=placeholder;
    textF.delegate = self;
    textF.font = [ViewTool getFont:16.0f];
    [textF setValue:UIColorFromRGB(0xe5e5e5) forKeyPath:@"_placeholderLabel.textColor"];
    [textF setValue:[ViewTool getFont:16.0f] forKeyPath:@"_placeholderLabel.font"];
    return textF;
}

- (void)clickToSelectBank:(ImageButton *)btn{
    
    switch (btn.tag) {
        case 10001://账户类型
        {
            isBank = NO;
            if(btn.selected == NO){
                
                [UIView animateWithDuration:0.5 animations:^{
                    _bankListView.hidden = NO;
                }];
                btn.selected = YES;
            }else{
                
                [UIView animateWithDuration:0.5 animations:^{
                    _bankListView.hidden =YES;
                }];
                btn.selected = NO;
            }
            [_bankPicker reloadAllComponents];

        }
            break;
        case 10002://银行列表
            
        {

            isBank = YES;
            if(btn.selected == NO){
                
                [UIView animateWithDuration:0.5 animations:^{
                    _bankListView.hidden = NO;
                }];
                btn.selected = YES;
            }else{
                [UIView animateWithDuration:0.5 animations:^{
                    _bankListView.hidden =YES;
                }];
                btn.selected = NO;
            }
            
            [_bankPicker reloadAllComponents];
            
        }
            break;
        case 121212://地区选择
        {
            if (_dictionary != nil) {
                
                AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
                geo.address = [NSString stringWithFormat:@"%@%@",_locationTF.text,_detailTF.text];
                //发起正向地理编码
                [_search AMapGeocodeSearch: geo];
            }
            
            if (btn.selected == NO) {
                [UIView animateWithDuration:0.25 animations:^{
                    _placeView.hidden = NO;
                }];
                
            }else{
                [UIView animateWithDuration:0.25 animations:^{
                    _placeView.hidden = YES;

                }];
            }
            btn.selected = !btn.selected;
            [_placePickerView reloadAllComponents];
            
        }
            break;
        default:
            break;
    }
}
- (void)hanldePlaceSelected:(UIButton *)sender{
    
    locationBtn.selected = NO;
    _placeView.hidden = YES;

    if (self.provinceArray.count > 0) {
        proStr = [self.provinceArray objectAtIndex:[_placePickerView selectedRowInComponent:0]][@"region"];
    }else{
        proStr = @"";
    }
    if (self.cityArray.count > 0) {
        city = [self.cityArray objectAtIndex:[_placePickerView selectedRowInComponent:1]][@"region"];
        
    }else{
        city = @"";
    }
    if (self.regionArray.count > 0) {
        regionStr = [self.regionArray objectAtIndex:[_placePickerView selectedRowInComponent:2]][@"region"];
        
        regionID = [[self.regionArray objectAtIndex:[_placePickerView selectedRowInComponent:2]][@"id"] intValue];
        NSLog(@"regionID:%d",regionID);
        if (regionID == 0) {
            [Tools showMessage:@"请重新选择地区" view:self];
        }
    }else{
        regionStr = @"";
    }
    if ([sender.currentTitle isEqualToString:@"完成"]) {
        
        if ([proStr isEqualToString:city]) {
            _locationTF.text = [NSString stringWithFormat:@"%@ %@",proStr,regionStr];
        }else{
            _locationTF.text = [NSString stringWithFormat:@"%@ %@ %@",proStr,city,regionStr];
        }
    }
    
}
#pragma mark
#pragma mark---------银行选择
- (void)creatBankListPickerView
{
    
    _bankListView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _bankListView.hidden = YES;
    [self.view addSubview:_bankListView];
    CGFloat pickerH = 260;
    
    UIView * bgBlackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenHeight - pickerH)];
    bgBlackView.backgroundColor = [UIColor blackColor];
    bgBlackView.alpha = 0.6;
    [_bankListView addSubview:bgBlackView];
    
    
    UIView *pView = [[UIView alloc] initWithFrame:CGRectMake(0, bgBlackView.maxY, kScreenWidth, pickerH)];
    pView.backgroundColor = [UIColor whiteColor];
    [_bankListView addSubview:pView];

    
    CGFloat buttonW = 60.0f;
    CGFloat buttonH = 40.0f;
    
    UIView *line = [ViewTool getLineViewWith:CGRectMake(0, 0, kScreenWidth, 0.8) withBackgroudColor:GrayLineColor];
    [pView addSubview:line];
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(pView.width - buttonW -10, 0, buttonW, buttonH)];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(clickSure:) forControlEvents:UIControlEventTouchUpInside];
    sureBtn.titleLabel.font= [ViewTool getFont:15.0f];
    [pView addSubview:sureBtn];
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, buttonW, buttonH)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:GRAYCOLOR forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(clickSure:) forControlEvents:UIControlEventTouchUpInside];
    cancelBtn.titleLabel.font= [ViewTool getFont:15.0f];
    [pView addSubview:cancelBtn];
    
    UIView *line1 = [ViewTool getLineViewWith:CGRectMake(0, cancelBtn.maxY, kScreenWidth, 0.8) withBackgroudColor:GrayLineColor];
    [pView addSubview:line1];
    
    _bankPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, line1.maxY+0.8, pView.width, pView.height-line1.maxY-70 -30)];
    _bankPicker.delegate = self;
    _bankPicker.dataSource = self;
    [pView addSubview:_bankPicker];
    
}
#pragma mark
#pragma mark---------地区选择
- (void)creatCityPickerView{
    
    _placeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _placeView.hidden = YES;
    [self.view addSubview:_placeView];
    
    CGFloat pickerH = 260;
    CGFloat buttonW = 60.0f;
    CGFloat buttonH = 40.0f;
    
    UIView * bgBlackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenHeight - pickerH)];
    bgBlackView.backgroundColor = [UIColor blackColor];
    bgBlackView.alpha = 0.6;
    [_placeView addSubview:bgBlackView];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapToHideBlackView:)];
    [bgBlackView addGestureRecognizer:tapGesture];
    
    UIView *pView = [[UIView alloc] initWithFrame:CGRectMake(0, bgBlackView.maxY, kScreenWidth, pickerH)];
    pView.backgroundColor = [UIColor whiteColor];
    [_placeView addSubview:pView];
    
    UIView *line1 = [ViewTool getLineViewWith:CGRectMake(0, 0 , kScreenWidth, 1) withBackgroudColor:GRAYLINECOLOR];
    [pView addSubview:line1];
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, line1.maxY , buttonW, buttonH)];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(hanldePlaceSelected:) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitleColor:GRAYCOLOR forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [ViewTool getFont:15.0f];
    [pView addSubview:leftBtn];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(pView.width - 10 - buttonW, line1.maxY, buttonW, buttonH)];
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [ViewTool getFont:15.0f];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(hanldePlaceSelected:) forControlEvents:UIControlEventTouchUpInside];
    [pView addSubview:rightBtn];
    UIView *line = [ViewTool getLineViewWith:CGRectMake(0, rightBtn.maxY, kScreenWidth, 1) withBackgroudColor:GRAYLINECOLOR];
    [pView addSubview:line];
    
    _placePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, line.maxY  , kScreenWidth, pView.height - 30-70)];
    [pView addSubview:_placePickerView];
    _placePickerView.delegate = self;
    _placePickerView.dataSource = self;
}
- (void)tapToHideBlackView:(UITapGestureRecognizer *)tap{
    
}
- (void)goToBack{
    
    ZLAlertView *alert = [[ZLAlertView alloc]initWithTitle:@"提示" message:@"是否需要返回"];
    [alert addBtnTitle:@"取消" action:^{
        
    }];
    
    [alert addBtnTitle:@"确定" action:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [alert showAlertWithSender:self];
}
- (void)goToBack222{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)clickSure:(UIButton *)btn{
    
    [UIView animateWithDuration:0.5 animations:^{
        _bankListView.hidden = YES;
    }];
    
    
    if ([btn.currentTitle isEqualToString:@"确定"]) {
        NSInteger row = [_bankPicker selectedRowInComponent:0];
        if (isBank == YES) {
            if (_bankListArr.count > 0) {
                _bankTF.text = _bankListArr[row][@"bank_name"];
                bankNameID = [_bankListArr[row][@"id"] intValue];
            }
            
        }else{
            if (_bankTypeArr.count > 0) {
                _typeTF.text = _bankTypeArr[row][@"title"];
                bankTypeID = [_bankTypeArr[row][@"id"] intValue];
            }
            
        }
        
    }else{
        
        
    }
    if (isBank == YES) {
        _bankListBtn.selected = NO;
    }else{
        _bankTypeBtn.selected = NO;
    }
}
#pragma mark -- pickView代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    if(pickerView ==_bankPicker){
        if (isBank == YES) {
            return 1;
        }else{
            return 1;
        }
    }else if (pickerView == _placePickerView){
        return 3;
    }
    return 0;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (pickerView == _bankPicker) {
        if (isBank == YES) {
            return _bankListArr.count;
        }else{
            return _bankTypeArr.count;
        }
        
    }else if(pickerView == _placePickerView){
        if (component ==0) {
            return _provinceArray.count;
        }else if(component == 1){
            return _cityArray.count;
        }else{
            return _regionArray.count;
        }
        
    }
    return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (pickerView == _bankPicker) {
        if (isBank == YES) {
            return _bankListArr[row][@"bank_name"];
        }else{
            return _bankTypeArr[row][@"title"];
        }
    }else{
        if (component == 0) {
            return _provinceArray[row][@"region"];
        }else if (component == 1) {
            return _cityArray[row][@"region"];
        }else{
            return _regionArray[row][@"region"];
        }
    }
    return nil;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (pickerView == _bankPicker) {

    }else {
        if (component == 0) {
            _selectedArray = self.provinceArray[row][@"city"];
            if (_selectedArray.count > 0) {
                self.cityArray = _selectedArray;
            }else{
                self.cityArray = nil;
            }
            if (self.cityArray.count > 0) {
                self.regionArray = _cityArray.firstObject[@"district"];
            } else {
                self.regionArray = nil;
            }
        }
    [pickerView selectedRowInComponent:1];
    [pickerView reloadComponent:1];
    [pickerView selectedRowInComponent:2];
    if (component == 1) {
        if (self.selectedArray.count > 0 && self.cityArray.count > 0) {
            self.regionArray = [self.selectedArray objectAtIndex:row][@"district"];
            //   self.cityArray = _provinceArray.firstObject[@"city"];
        } else {
            self.regionArray = nil;
        }
        [pickerView selectRow:1 inComponent:2 animated:YES];
    }
    [pickerView reloadComponent:2];
    
    }
    
}
#pragma mark
#pragma mark ------提交
-(void)referClick:(UIButton *)sender
{
  
    if (_propertyTF.text == nil | [_propertyTF.text length] == 0 ){
        [self.view makeToast:@"请填写物业名称"];
        return;
    }else if (_nameTF.text == nil | [_nameTF.text length] == 0 ){
        [self.view makeToast:@"请填写联系人"];
        return;
    }else if (_telTF.text == nil | [_telTF.text length] == 0 ){
        [self.view makeToast:@"请填写联系电话"];
        return;
    }else if (![JudgeNumber boolenPhone:_telTF.text]){
        [Tools showMessage:@"请填写正确的联系电话" view:self];
        return;
    }else if (_locationTF.text == nil | [_locationTF.text length] == 0 ){
        [self.view makeToast:@"请选择地区"];
        return;
    }else if (_detailTF.text == nil | [_detailTF.text length] == 0 ){
        [self.view makeToast:@"请填写详细地址"];
        return;
    }else if (_typeTF.text == nil | [_typeTF.text length] == 0 ){
        [self.view makeToast:@"请选择账户类型"];
        return;
    }else if (_bankTF.text == nil | [_bankTF.text length] == 0 ){
        [self.view makeToast:@"请选择所属银行"];
        return;
    }else if (_bankIDTF.text == nil | [_bankIDTF.text length] == 0 ){
        [self.view makeToast:@"请填写银行账号"];
        return;
    }else if ([_bankIDTF.text length] < 16 || [_bankIDTF.text length] > 19){
        [self.view makeToast:@"请填写正确的银行卡号"];
        return;
    }else if (_houseHolderTF.text == nil | [_houseHolderTF.text length] == 0 ){
        [self.view makeToast:@"请填写银行户名"];
        return;
    }else if (_addrTF.text == nil | [_addrTF.text length] == 0 ){
        [self.view makeToast:@"请填写开户银行详细地址"];
        return;
    }

    NSString *lon = [NSString stringWithFormat:@"%f",lontitu];
    NSString *lat = [NSString stringWithFormat:@"%f",latitu];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"app.property.store" forKey:@"method"];
    [dic setObject:_propertyTF.text forKey:@"title"];
    [dic setObject:@(regionID) forKey:@"district_id"];//区县id
    [dic setObject:_telTF.text forKey:@"tel"];
    [dic setObject:_detailTF.text forKey:@"address"];
    [dic setObject:lon forKey:@"longitude"];
    [dic setObject:lat forKey:@"latitude"];
    [dic setObject:_nameTF.text forKey:@"contact"];
    [dic setObject:_houseHolderTF.text forKey:@"payer"];
    [dic setObject:@(bankNameID) forKey:@"bank_id"];
    [dic setObject:_bankIDTF.text forKey:@"account"];
    [dic setObject:@(bankTypeID) forKey:@"account_type"];
    [dic setObject:_addrTF.text forKey:@"belong_to"];
    
    
    NSLog(@"%f  -----jingweidu----- %f ",lontitu,latitu);
    [SVProgressHUD show];
    self.navigationItem.rightBarButtonItem = nil;
    [DataTool postWithUrl:HEAD_URL parameters:[DataTool addSignToDic:dic] success:^(id data) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(referClick:)];
        [SVProgressHUD dismiss];
        NSDictionary *jsonDic = YGJsonParserWithData(data);
        NSLog(@"%@新建物业返回的参数",jsonDic);
        if ([jsonDic[@"status"] isEqualToString:@"SUCCESS"]) {
            //            [SVProgressHUD showSuccessWithStatus:@"新建成功"];
            [_delegate passValue:@"SUCCESS"];
            [self performSelector:@selector(goToBack222) withObject:nil afterDelay:0.5];
        }else{
            
            [Tools showMessage:jsonDic[@"msg"] view:self];
        }
    } fail:^(NSError *error) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(referClick:)];
        [SVProgressHUD dismiss];
        NSLog(@"%@",error.localizedDescription);
    }];
}

#pragma
#pragma mark ---------正地理编码
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response{
    NSLog(@"%lu",(unsigned long)response.geocodes.count);
    if (response.geocodes.count == 0){
        return;
    }
    AMapGeocode *geo = response.geocodes[0];
    lontitu = geo.location.longitude;
    latitu = geo.location.latitude;
    NSLog(@"%f2323223232332",lontitu);
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
//    if (_dictionary.count > 0) {
//        if (textField == _detailTF && _detailTF.text != nil) {
//            //构造AMapGeocodeSearchRequest对象，address为必选项，city为可选项
//            AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
//            geo.address = [NSString stringWithFormat:@"%@%@",_locationTF.text,_detailTF.text];
//            //发起正向地理编码
//            [_search AMapGeocodeSearch: geo];
//        }
//    }else{
    if (textField == _detailTF && _detailTF.text != nil) {
            //构造AMapGeocodeSearchRequest对象，address为必选项，city为可选项
            AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
            geo.address = [NSString stringWithFormat:@"%@%@",_locationTF.text,_detailTF.text];
            //发起正向地理编码
            [_search AMapGeocodeSearch: geo];
    }
//    }
}


#pragma mark
#pragma mark -----textField协议方法
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
